package org.delphy.crimson
{
	import caurina.transitions.Tweener;
	
	import flash.display.DisplayObject;
	
	import mx.containers.Canvas;
	import mx.controls.Image;
	import mx.core.Application;
	import mx.managers.PopUpManager;
	
	public class CrimsonPopUpWindow extends Canvas
	{
		private var targetWidth:Number;
		private var targetHeight:Number;
		
		[Embed('images/WindowFlorid.png')]
		private var floridClass:Class;
		
		private var florid:Image;
		private var content:Canvas;
		
		public function CrimsonPopUpWindow()
		{
			super();
			
			clipContent = false;
			styleName = "crimsonPopUpWindow";
		}
		
		override protected function createChildren():void{			
			florid = new Image();
			florid.source = new floridClass();
			florid.scaleContent = true;
			florid.blendMode = "multiply";
			florid.alpha = 0.3;
			florid.setStyle("verticalAlign", "bottom");
			addChild(florid);
			
			content = new Canvas();
			addChild(content);	
			
			super.createChildren();
		}
		
		override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void{
			super.updateDisplayList(unscaledWidth, unscaledHeight);
			
			florid.height = unscaledHeight + 50;
			florid.x = -70;
			florid.y = unscaledHeight - florid.height;
			
			content.width = unscaledWidth;
			content.height = unscaledHeight;
		}
		
		public function popUp():void{
			targetWidth = width;
			targetHeight = height;
			
			rotationX = 88;
			width = 0;
			
			PopUpManager.addPopUp(this, DisplayObject(Application.application), false);
			PopUpManager.centerPopUp(this);
			
			Tweener.addTween(this, {width: targetWidth,
				x: Application.application.width / 2 - targetWidth / 2,
				time: 0.5, 
				onComplete:onAnimationStage1Complete});	
		}
		
		private function onAnimationStage1Complete():void{
			Tweener.addTween(this, {
				rotationX: 0, 
				time: 1});
			
		}
		
		public function close():void{
			Tweener.addTween(this, 
				{width: 0,
				height: 0,
				alpha: 0,
				z: 1000,
			    x: Application.application.width / 2,
				y: Application.application.height / 2,
				time: 1,
				onComplete: onCloseAnimationComplete});
		}
		
		private function onCloseAnimationComplete():void{
			PopUpManager.removePopUp(this);
		}		
	}
}